/*!
 * avangard/character.js
 * © 2009 avangardonline.ru
 * @author BlooDHounD <http://www.controller.by>
 */

if ( !window.controller ) throw new Error( '"controller" not initialized' );

controller.require( 'avangard' );

if ( !avangard.Character ) {

	controller.require( 'controller.events.EventDispatcher' );

	/**
	 * @class
	 * @namespace	controller
	 * @extends		controller.events.EventDispatcher
	 * @author		BlooDHounD	<http://www.controller.by>
	 */
	avangard.Character = ( function() {

		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------

		var	$ =	controller,

			properties = new Array(
				'sex',
				'race',
				'level',
				'clanID',
				'clanName',
				'propensity'
			),
			l = properties.length,

			UPDATE = 'update';

		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------

		/**
		 * @constructor
		 * @param	{Number}	id
		 * @param	{String}	name
		 */
		var Character = function(id, name) {
			Character.superPrototype.constructor.call( this );
			this.id = id;
			this.name = name;
		};

		$.extend( Character, $.events.EventDispatcher );

		var CharacterPrototype = Character.prototype;

		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------

		/**
		 * @property
		 * @type	{Number}
		 */
		CharacterPrototype.id = null;

		/**
		 * @property
		 * @type	{String}
		 */
		CharacterPrototype.name = null;

		/**
		 * @property
		 * @type	{Boolean}
		 */
		CharacterPrototype.sex = false;

		/**
		 * @property
		 * @type	{Number}
		 */
		CharacterPrototype.race = null;

		/**
		 * @property
		 * @type	{Number}
		 */
		CharacterPrototype.level = 0;

		/**
		 * @property
		 * @type	{Number}
		 */
		CharacterPrototype.clanID = 0;

		/**
		 * @property
		 * @type	{Number}
		 */
		CharacterPrototype.clanName = null;

		/**
		 * @property
		 * @type	{Number}
		 */
		CharacterPrototype.propensity = 0;

		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------

		/**
		 * @method
		 * @param	{avangard.Character}	o
		 */
		CharacterPrototype.copyFrom = function(o) {
			if ( this.id != o.id || this.name != o.name ) {
				throw new Error( 'wrong object' );
			}
			var c = false,
				i,
				n;
			for ( i=0; i<l; i++ ) {
				n = properties[ i ];
				if ( this[ n ] != o[ n ] ) {
					this[ n ] = o[ n ];
					c = true;
				}
			}
			if ( c ) this.update();
		};

		/**
		 * @method
		 */
		CharacterPrototype.update = function() {
			if ( this.hasEventListener( UPDATE ) ) {
				this.dispatchEvent( new $.events.Event( UPDATE ) );
			}
		};

		/**
		 * @method
		 * @return	{String}
		 */
		CharacterPrototype.toString = function() {
			return '[Character id=' + this.id + ' name="' + this.name + '"]';
		};

		//--------------------------------------------------------------------------
		//
		//  Class methods
		//
		//--------------------------------------------------------------------------

		/**
		 * @static
		 * @method
		 * @return	{String}
		 */
		Character.toString = function() {
			return '[class Character]';
		};

		return Character;

	}() );

}
